package com.chinasofti.proxy2;

import com.sun.scenario.effect.impl.prism.PrDrawable;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

interface ClothFactory{//服装厂接口
    void productCloth();//生产服装
}

//李宁服装厂
class LiNingCloth  implements ClothFactory{
    @Override
    public void productCloth() {
        System.out.println("李宁服装厂生产了一批衣服");
    }
}
//代理  --  销售商
class  ProxyDaiLi implements InvocationHandler{

    Object object;//被代理的商品 变成了不确定的类型
    public  Object ProxyDaiLi(Object object){
        this.object=object;
        //返回代理对象
        return  Proxy.newProxyInstance
                (object.getClass().getClassLoader(),object.getClass().getInterfaces(),this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object returnval = method .invoke(object, args);
        return null;
    }
}
public class Proxy2 {
    public static void main(String[] args) {
        //1.代理李宁
        LiNingCloth ln = new LiNingCloth();
        ProxyDaiLi daili = new ProxyDaiLi();
        //Object object = daili.ProxyDaiLi(ln);
        ClothFactory dailishang = (ClothFactory) daili.ProxyDaiLi(ln);
        dailishang.productCloth();
        //2.代理袜子
        LangshaSockFactory ls = new LangshaSockFactory();
        SockFactory sf = (SockFactory)daili.ProxyDaiLi(ls);
        sf.producSock();
    }
    }
//袜子
interface  SockFactory{
    void producSock();
}
//袜子工厂
class LangshaSockFactory implements  SockFactory{

    @Override
    public void producSock() {
        System.out.println("来自浪莎袜子");
    }
}